package com.sailfish.springbootdemo.service.db2.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sailfish.springbootdemo.configs.UserHeaderHolder;
import com.sailfish.springbootdemo.dao.db2.PcbApproveConfigDao;
import com.sailfish.springbootdemo.dao.db2.PcbParentFactoryInfoDao;
import com.sailfish.springbootdemo.dao.db2.PcbSonFactoryInfoDao;
import com.sailfish.springbootdemo.dao.db4.UserDao;
import com.sailfish.springbootdemo.pojo.Result;
import com.sailfish.springbootdemo.pojo.db2.PcbApproveConfig;
import com.sailfish.springbootdemo.pojo.db2.PcbSonFactoryInfo;
import com.sailfish.springbootdemo.pojo.db4.Operate;
import com.sailfish.springbootdemo.pojo.db4.User;
import com.sailfish.springbootdemo.service.db2.PcbApproveConfigService;
import com.sailfish.springbootdemo.service.db4.OperateService;
import com.sailfish.springbootdemo.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.Date;
import java.util.List;

@Service
public class PcbApproveConfigServiceImpl implements PcbApproveConfigService {

    @Autowired
    PcbApproveConfigDao pcbApproveConfigDao;
    @Autowired
    PcbParentFactoryInfoDao pcbParentFactoryInfoDao;
    @Autowired
    PcbSonFactoryInfoDao pcbSonFactoryInfoDao;
    @Autowired
    OperateService operateService;
    @Autowired
    UserDao userDao;

    @Override
    public List<PcbApproveConfig> getList(PcbApproveConfig param) {
        List<PcbApproveConfig> allByLimit = pcbApproveConfigDao.getAllByLimit(param);
        for (int i = 0; i < allByLimit.size(); i++) {
            PcbApproveConfig item = allByLimit.get(i);
//            if(item.getSonFactoryId() != null){
//                PcbSonFactoryInfo pcbSonFactoryInfo = pcbSonFactoryInfoDao.getById(item.getSonFactoryId());
//                item.setPcbSonFactoryInfo(pcbSonFactoryInfo);
//            }
            if (item.getOperator() != null) {
                User userInfo = userDao.getUserInfoById(item.getOperator());
                item.setOperatorInfo(userInfo);
            }
            if (item.getOperator2() != null) {
                User userInfo = userDao.getUserInfoById(item.getOperator2());
                item.setOperatorInfo2(userInfo);
            }
            if (item.getOperator3() != null) {
                User userInfo = userDao.getUserInfoById(item.getOperator3());
                item.setOperatorInfo3(userInfo);
            }
        }
        return allByLimit;
    }

    @Transactional(transactionManager = "MysqlTransactionManager2")
    @Override
    public Result insertOrUpdate(String data) {
        try {
            String userId = UserHeaderHolder.getUserId();
            JSONObject jsonObject = JSONObject.parseObject(data);
            Integer id = jsonObject.getInteger("id");
            Integer flowType = jsonObject.getInteger("flowType");
            String approveNode = jsonObject.getString("approveNode");
            JSONArray sonFactoryIds = jsonObject.getJSONArray("sonFactoryIds");
            Integer parentFactoryId = jsonObject.getInteger("parentFactoryId");
            Integer operator = jsonObject.getInteger("operator");
            Integer operator2 = jsonObject.getInteger("operator2");
            Integer operator3 = jsonObject.getInteger("operator3");
            String remark = jsonObject.getString("remark");
            String flowTypeName = "";
            if (flowType == 2) {
                flowTypeName = "新技术跟踪";
            }
            if (flowType == 3) {
                flowTypeName = "能力数据调查";
            }
            if (flowType == 4) {
                flowTypeName = "开放能力数据审核";
            }
            if (flowType == 5) {
                flowTypeName = "能力数据修改";
            }
            if (flowType != 2) {
                if (sonFactoryIds == null || sonFactoryIds.size() == 0) {
                    return ResultUtil.error(500, "参数sonFactoryIds为空", "param sonFactoryIds is null", null);
                }
                if (id == null) {
                    for (int i = 0; i < sonFactoryIds.size(); i++) {
                        PcbApproveConfig pcbApproveConfig = new PcbApproveConfig();
                        pcbApproveConfig.setFlowTypeName(flowTypeName);
                        Integer sonFactoryId = (Integer) sonFactoryIds.get(i);
                        final PcbSonFactoryInfo byId = pcbSonFactoryInfoDao.getById(sonFactoryId);
                        parentFactoryId = byId.getParentId();
                        pcbApproveConfig.setSonFactoryName(byId.getSonFactoryName());
                        pcbApproveConfig.setParentFactoryName(byId.getCompanyName());
                        pcbApproveConfig.setApproveNode(approveNode);
                        pcbApproveConfig.setFlowType(flowType);
                        pcbApproveConfig.setParentFactoryId(parentFactoryId);
                        pcbApproveConfig.setSonFactoryId(sonFactoryId);
                        pcbApproveConfig.setRemark(remark);
                        pcbApproveConfig.setOperator(operator);
                        pcbApproveConfig.setOperator2(operator2);
                        pcbApproveConfig.setOperator3(operator3);
                        PcbApproveConfig oneByLimit = pcbApproveConfigDao.getOneByLimit(flowType, null, sonFactoryId);
                        if (oneByLimit != null) {
                            return ResultUtil.error(500, "该流程已存在", "data exist", null);
                        }
                        pcbApproveConfig.setCreateTime(new Date());
                        pcbApproveConfig.setCreateUser(userId);
                        int count = pcbApproveConfigDao.insert(pcbApproveConfig);
                        if (count > 0) {
                            operateService.addOperate(new Operate(2, Integer.parseInt(userId), "PCB审核人员配置", 1, 1, new Date(), null, JSONObject.toJSONString(pcbApproveConfig)));
                        } else {
                            return ResultUtil.error(500, "操作失败", "Operate Fail", null);
                        }
                    }
                    return ResultUtil.success(null);
                } else {
                    PcbApproveConfig pcbApproveConfig = new PcbApproveConfig();
                    pcbApproveConfig.setFlowTypeName(flowTypeName);
                    Integer sonFactoryId = (Integer) sonFactoryIds.get(0);
                    final PcbSonFactoryInfo byId = pcbSonFactoryInfoDao.getById(sonFactoryId);
                    parentFactoryId = byId.getParentId();
                    pcbApproveConfig.setSonFactoryName(byId.getSonFactoryName());
                    pcbApproveConfig.setParentFactoryName(byId.getCompanyName());
                    pcbApproveConfig.setApproveNode(approveNode);
                    pcbApproveConfig.setFlowType(flowType);
                    pcbApproveConfig.setParentFactoryId(parentFactoryId);
                    pcbApproveConfig.setSonFactoryId(sonFactoryId);
                    pcbApproveConfig.setRemark(remark);
                    pcbApproveConfig.setOperator(operator);
                    pcbApproveConfig.setOperator2(operator2);
                    pcbApproveConfig.setOperator3(operator3);
                    PcbApproveConfig oldData = pcbApproveConfigDao.getById(id);
                    pcbApproveConfig.setId(id);
                    pcbApproveConfig.setUpdateTime(new Date());
                    pcbApproveConfig.setUpdateUser(userId);
                    int count = pcbApproveConfigDao.update(pcbApproveConfig);
                    if (count > 0) {
                        PcbApproveConfig newData = pcbApproveConfigDao.getById(id);
                        operateService.addOperate(new Operate(2, Integer.parseInt(userId), "PCB审核人员配置", 3, 1, new Date(), JSONObject.toJSONString(oldData), JSONObject.toJSONString(newData)));
                    } else {
                        return ResultUtil.error(500, "操作失败", "Operate Fail", null);
                    }
                    return ResultUtil.success(null);
                }
            } else {
                if (id == null) {
                    PcbApproveConfig pcbApproveConfig = new PcbApproveConfig();
                    pcbApproveConfig.setFlowTypeName(flowTypeName);
                    pcbApproveConfig.setApproveNode(approveNode);
                    pcbApproveConfig.setFlowType(flowType);
                    pcbApproveConfig.setRemark(remark);
                    pcbApproveConfig.setOperator(operator);
                    pcbApproveConfig.setOperator2(operator2);
                    pcbApproveConfig.setOperator3(operator3);
                    PcbApproveConfig oneByLimit = pcbApproveConfigDao.getOneByLimit(flowType, null, null);
                    if (oneByLimit != null) {
                        return ResultUtil.error(500, "该流程已存在", "data exist", null);
                    }
                    pcbApproveConfig.setCreateTime(new Date());
                    pcbApproveConfig.setCreateUser(userId);
                    int count = pcbApproveConfigDao.insert(pcbApproveConfig);
                    if (count > 0) {
                        operateService.addOperate(new Operate(2, Integer.parseInt(userId), "PCB审核人员配置", 1, 1, new Date(), null, JSONObject.toJSONString(pcbApproveConfig)));
                    } else {
                        return ResultUtil.error(500, "操作失败", "Operate Fail", null);
                    }
                    return ResultUtil.success(null);
                } else {
                    PcbApproveConfig pcbApproveConfig = new PcbApproveConfig();
                    pcbApproveConfig.setFlowTypeName(flowTypeName);
                    pcbApproveConfig.setApproveNode(approveNode);
                    pcbApproveConfig.setFlowType(flowType);
                    pcbApproveConfig.setParentFactoryId(parentFactoryId);
                    pcbApproveConfig.setRemark(remark);
                    pcbApproveConfig.setOperator(operator);
                    pcbApproveConfig.setOperator2(operator2);
                    pcbApproveConfig.setOperator3(operator3);
                    PcbApproveConfig oldData = pcbApproveConfigDao.getById(id);
                    pcbApproveConfig.setId(id);
                    pcbApproveConfig.setUpdateTime(new Date());
                    pcbApproveConfig.setUpdateUser(userId);
                    int count = pcbApproveConfigDao.update(pcbApproveConfig);
                    if (count > 0) {
                        PcbApproveConfig newData = pcbApproveConfigDao.getById(id);
                        operateService.addOperate(new Operate(2, Integer.parseInt(userId), "PCB审核人员配置", 3, 1, new Date(), JSONObject.toJSONString(oldData), JSONObject.toJSONString(newData)));
                    } else {
                        return ResultUtil.error(500, "操作失败", "Operate Fail", null);
                    }
                    return ResultUtil.success(null);
                }
            }
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResultUtil.error(500, e.getMessage(), e.getMessage(), null);
        }

    }

    @Transactional(transactionManager = "MysqlTransactionManager2")
    @Override
    public Result delete(Integer id) {
        try {
            String userId = UserHeaderHolder.getUserId();
            if (id == null) {
                return ResultUtil.error(500, "参数id为空", "param id is null", null);
            }
            PcbApproveConfig oldData = pcbApproveConfigDao.getById(id);
            int count = pcbApproveConfigDao.deleteById(id);
            if (count > 0) {
                operateService.addOperate(new Operate(2, Integer.parseInt(userId), "PCB审核人员配置", 2, 1, new Date(), JSONObject.toJSONString(oldData), null));
            } else {
                return ResultUtil.error(500, "操作失败", "Operate Fail", null);
            }
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResultUtil.error(500, e.getMessage(), e.getMessage(), null);
        }
        return ResultUtil.success(null);
    }
}
